<template>
  <div style="margin-top: 50px">
    <el-form
      ref="Formone"
      :model="value"
      :rules="rules"
      label-width="150px"
      style="width: 500px"
    >
      <el-form-item label="商品分类:">
        <div class="block">
          <el-cascader
            v-model="productCateValue"
            :options="options"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"
          ></el-cascader>
        </div>
      </el-form-item>

      <el-form-item label="商品名称：" prop="name">
        <el-input v-model="value.name"></el-input>
      </el-form-item>

      <el-form-item label="副标题：" prop="subTitle">
        <el-input v-model="value.subTitle"></el-input>
      </el-form-item>

      <el-form-item label="商品货号：">
        <el-input v-model="value.productSn"></el-input>
      </el-form-item>

      <el-form-item label="市场价：">
        <el-input v-model="value.originalPrice"></el-input>
      </el-form-item>

      <el-form-item label="商品售价：">
        <el-input v-model="value.price"></el-input>
      </el-form-item>

      <el-form-item label="商品库存：">
        <el-input v-model="value.store"></el-input>
      </el-form-item>

      <el-form-item label="排序：">
        <el-input v-model="value.sort"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" size="mini" @click="xiangxia"
          >下一步，填写商品促销</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getCategoryWithChildren,
  getCategoryId,
} from "../../../../../api/categoryApi";
export default {
  data() {
    return {
      rules: {
        name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
        subTitle: [
          { required: true, message: "请输入商品副标题", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
      },

      productCateValue: [], //代表选中的那个分类
      options: [], //得到的数据重新处理过后
    };
  },
  computed: {
    // productId是当前商品的ID
    productId() {
      return this.value.id;
    },
  },
  watch: {

    //编辑分类回显
    productId(newValue) {
      if (this.$route.query.id == null) {
        return;
      }
      if(newValue === undefined || newValue === null || newValue === 0){
        return
      }
      if(this.value.productCategoryId !== null){
          if(this.value.cateParentId && this.value.cateParentId !== 0){
            // 说明是一个二级分类
            this.productCateValue.push(this.value.cateParentId)
          }
          // 说明是一级分类
          this.productCateValue.push(this.value.productCategoryId)
        }
    },
    //
    productCateValue(newValue) {
      if (newValue.length == 1) {
        this.value.productCategoryId = newValue[0];
        getCategoryId(newValue[0]).then((res) => {
          console.log(res);
          this.value.productCategoryName = res.data[0].name;
        });
      }
      if (newValue.length == 2) {
        this.value.productCategoryId = newValue[1];
        getCategoryId(newValue[1]).then((res) => {
          console.log(res);
          this.value.productCategoryName = res.data[0].name;
        });
      }
    },
  },
  created() {
    getCategoryWithChildren().then((res) => {
      console.log(res);
      if (res.status == 1) {
        let datadata = res.data;
        this.options = [];
        for (let i = 0; i < datadata.length; i++) {
          let children = [];
          if (datadata[i].children !== null) {
            for (let j = 0; j < datadata[i].children.length; j++) {
              children.push({
                value: datadata[i].children[j].id,
                label: datadata[i].children[j].name,
              });
            }
          }
          children = children.length > 0 ? children : null;
          this.options.push({
            value: datadata[i].id,
            label: datadata[i].name,
            children: children,
          });
        }
      }
    });
  },
  props: {
    value: Object,
  },
  methods: {
    handleChange() {},
    xiangxia() {
      this.$emit("xiabu");
    },
  },
};
</script>

<style>
</style>